package com.itheima.reggie.service.impl;

import com.itheima.reggie.commons.R;
import com.itheima.reggie.dao.LoginDao;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;
    @Override
    public R<Employee> login(Employee employee) {
        Employee dbEmployee = loginDao.findByName(employee);
        //根据用户名没法找到用户
        if(dbEmployee==null){
            return R.error("用户名不存在");
        }

        //对比密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

        Boolean bool = encoder.matches(employee.getPassword(), dbEmployee.getPassword());
        if(!bool){
            return  R.error("密码错误");
        }

        //检查是否为禁用的用户
        if(dbEmployee.getStatus()==0){
            return R.error("账号被禁用了");
        }
        //登陆成功
        return R.success(dbEmployee);
    }
}
